package com.css.fxfzysx.modules.lifeline.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.attachment.service.AttachmentInfoService;
import com.css.fxfzysx.base.dict.service.DictService;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.modules.lifeline.dto.LifelineDto;
import com.css.fxfzysx.modules.lifeline.entity.basicLibrary.*;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxUpNetworkEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.param.LifelineFoundationLibraryParam;
import com.css.fxfzysx.modules.lifeline.param.updateBasicLibraryParam;
import com.css.fxfzysx.modules.lifeline.repository.LifelineEngineeringRepository;
import com.css.fxfzysx.modules.lifeline.repository.LifelineFoundationLibraryRepository;
import com.css.fxfzysx.modules.lifeline.service.LifelineFoundationLibraryService;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.utils.MainTableUtils;
import com.css.fxfzysx.util.CreateGson;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import com.css.fxfzysx.zcpt.sys.entity.SUser;
import com.css.fxfzysx.zcpt.sys.service.SUserService;
import com.css.fxfzysx.zcpt.sys.service.SysAreaService;
import com.css.fxfzysx.zcpt.sys.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;

@Service
public class LifelineFoundationLibraryServiceImpl implements LifelineFoundationLibraryService {

    @Autowired
    private LifelineFoundationLibraryRepository lifelineFoundationLibraryRepository;

    @Autowired
    private SUserService sUserService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysAreaService sysAreaService;

    @Autowired
    private LifelineEngineeringRepository lifelineEngineeringRepository;

    @Autowired
    private DictService dictService;

    @Override
    public RestResponse upload(Integer type, String province, String city, String county, MultipartFile file) throws Exception {
        RestResponse res = null;
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        String user = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);

        LifelineDto dto = new LifelineDto();
        dto.setUserName(sUser.getUserName());
        Integer role = getRole();
        dto.setRole(role);
        if(role == 1){
            dto.setProvince(province);
            dto.setProvinceName(sysAreaService.getAreaNameByAreaId(province));
            dto.setCity(city);
            dto.setCityName(sysAreaService.getAreaNameByAreaId(city));
            dto.setCounty(county);
            dto.setCountyName(sysAreaService.getAreaNameByAreaId(county));
        }
        dto.setReader(reader);

        if (type == 1) {
            //地下管网
            res = uploadUpNetwork(dto);
        } else if (type == 2) {
            //水池
            res = uploadPool(dto);
        } else if (type == 3) {
            //燃气储气罐
            res = uploadGasStorageTank(dto);
        } else if (type == 4) {
            //桥梁
            res = uploadBridge(dto);
        } else if (type == 5) {
            //道路
            res = uploadRoad(dto);
        } else if (type == 6) {
            //变电站
            res = uploadTransformerSubstation(dto);
        } else if (type == 7) {
            //电线电缆
            res = uploadWire(dto);
        } else if (type == 8) {
            //通信设备
            res = uploadDevices(dto);
        }
        return res;
    }

    private String getKeyByValue(String code, String value) {
        if (!PlatformObjectUtils.isEmpty(value)) {
            String key = dictService.getKeyByDictCodeAndValue(code, value.trim());
            return key;
        }
        return null;
    }

    private RestResponse uploadBridge(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 结构形式, 场地类别, 设防情况, 建造年代, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = bridgeVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryBridgeEntity entity = new BasicLibraryBridgeEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setStructuralStyle(getKeyByValue("smxqlsbjg",objects.get(1).toString()));
            entity.setSiteClassification(getKeyByValue("smxcdlb",objects.get(2).toString()));
            entity.setFortification(getKeyByValue("smxsfqk",objects.get(3).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(4).toString()));
            entity.setDisasterRate6State(objects.get(4).toString());
            entity.setDisasterRate7State(objects.get(5).toString());
            entity.setDisasterRate8State(objects.get(6).toString());
            entity.setDisasterRate9State(objects.get(7).toString());
            entity.setDisasterRate10State(objects.get(8).toString());
            int count = lifelineFoundationLibraryRepository.getBridgeCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveBridge(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse bridgeVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{结构形式}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{场地类别}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{设防情况}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(8).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(9).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadWire(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 电压等级, 场地类别, 建造年代, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = wireVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryWireEntity entity = new BasicLibraryWireEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setVoltageLevel(getKeyByValue("smxdlxldydj",objects.get(1).toString()));
            entity.setSiteClassification(getKeyByValue("smxcdlb",objects.get(2).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(3).toString()));
            entity.setDisasterRate6State(objects.get(4).toString());
            entity.setDisasterRate7State(objects.get(5).toString());
            entity.setDisasterRate8State(objects.get(6).toString());
            entity.setDisasterRate9State(objects.get(7).toString());
            entity.setDisasterRate10State(objects.get(8).toString());
            int count = lifelineFoundationLibraryRepository.getWireCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveWire(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse wireVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{电压等级}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{场地类别}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(8).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadDevices(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 所在楼层, 安装情况, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = devicesVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryDevicesEntity entity = new BasicLibraryDevicesEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setFloor(getKeyByValue("smxtxsbszlc",objects.get(1).toString()));
            entity.setInstallationSituation(getKeyByValue("smxtxsbazqk",objects.get(2).toString()));
            entity.setDisasterRate6State(objects.get(3).toString());
            entity.setDisasterRate7State(objects.get(4).toString());
            entity.setDisasterRate8State(objects.get(5).toString());
            entity.setDisasterRate9State(objects.get(6).toString());
            entity.setDisasterRate10State(objects.get(7).toString());
            int count = lifelineFoundationLibraryRepository.getDevicesCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveDevices(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse devicesVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{所在楼层}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{安装情况}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadGasStorageTank(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 结构形式, 场地类别, 建造年代, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = gasVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryGasEntity entity = new BasicLibraryGasEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setStructuralStyle(getKeyByValue("smxrqjgxs",objects.get(1).toString()));
            entity.setSiteClassification(getKeyByValue("smxcdlb",objects.get(2).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(3).toString()));
            entity.setDisasterRate6State(objects.get(4).toString());
            entity.setDisasterRate7State(objects.get(5).toString());
            entity.setDisasterRate8State(objects.get(6).toString());
            entity.setDisasterRate9State(objects.get(7).toString());
            entity.setDisasterRate10State(objects.get(8).toString());
            int count = lifelineFoundationLibraryRepository.getGasCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveGas(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse gasVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{结构形式}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{场地类别}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(8).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadRoad(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 场地类别, 设防情况, 建造年代, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = roadVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryRoadEntity entity = new BasicLibraryRoadEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
           if(dto.getRole() == 1){
               entity.setProvince(dto.getProvince());
               entity.setProvinceName(dto.getProvinceName());
               entity.setCity(dto.getCity());
               entity.setCityName(dto.getCityName());
               entity.setCounty(dto.getCounty());
               entity.setCountyName(dto.getCountyName());
           }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setType(getKeyByValue("smxcdlb",objects.get(1).toString()));
            entity.setIntensity(getKeyByValue("smxsfqk",objects.get(2).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(3).toString()));
            entity.setDisasterRate6State(objects.get(4).toString());
            entity.setDisasterRate7State(objects.get(5).toString());
            entity.setDisasterRate8State(objects.get(6).toString());
            entity.setDisasterRate9State(objects.get(7).toString());
            entity.setDisasterRate10State(objects.get(8).toString());
            int count = lifelineFoundationLibraryRepository.getRoadCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveRoad(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse roadVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{场地类别}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{设防情况}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(8).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadPool(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 水池形状, 设防情况, 建造年代, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = poolVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryPoolEntity entity = new BasicLibraryPoolEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setPoolShape(getKeyByValue("smxgsscxz",objects.get(1).toString()));
            entity.setIntensity(getKeyByValue("smxsfqk",objects.get(2).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(3).toString()));
            entity.setDisasterRate6State(objects.get(4).toString());
            entity.setDisasterRate7State(objects.get(5).toString());
            entity.setDisasterRate8State(objects.get(6).toString());
            entity.setDisasterRate9State(objects.get(7).toString());
            entity.setDisasterRate10State(objects.get(8).toString());
            int count = lifelineFoundationLibraryRepository.getPoolCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.savePool(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse poolVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{水池形状}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{设防情况}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(8).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadTransformerSubstation(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 设备类型, 建造年代, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = transformerSubstationVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibrarySubstationEntity entity = new BasicLibrarySubstationEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setType(getKeyByValue("smxbdzsblx",objects.get(1).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(2).toString()));
            entity.setDisasterRate6State(objects.get(3).toString());
            entity.setDisasterRate7State(objects.get(4).toString());
            entity.setDisasterRate8State(objects.get(5).toString());
            entity.setDisasterRate9State(objects.get(6).toString());
            entity.setDisasterRate10State(objects.get(7).toString());
            int count = lifelineFoundationLibraryRepository.getTransformerSubstationCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveTransformerSubstation(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse transformerSubstationVerification(int size, List<List<Object>> read) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = read.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{设备类型}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    private RestResponse uploadUpNetwork(LifelineDto dto) {
        ExcelReader reader = dto.getReader();
        List<Object> title = reader.readRow(2);
        String titleName = "[序号, 场地类别, 管材, 接口形式, 管径（mm）, 建造年代, 设防情况, Ⅵ度破坏状态, Ⅶ度破坏状态, Ⅷ度破坏状态, Ⅸ度破坏状态, Ⅹ度破坏状态]";
        if (!titleName.equals(title.toString())) {
            return RestResponse.fail("导入失败，请检查表头是否改动");
        }
        List<List<Object>> read = reader.read(4, reader.getRowCount());
        if (read.size() == 0) {
            return RestResponse.fail("导入失败，表格中无数据");
        }
        //数据校验
        RestResponse rs = upNetworkVerification(read.size(), read);
        if (rs.getCode() != 200) {
            return RestResponse.fail(rs.getMessage());
        }
        int i = 5;
        ArrayList<Integer> list = new ArrayList<>();
        for (List<Object> objects : read) {
            BasicLibraryNetworkEntity entity = new BasicLibraryNetworkEntity();
            entity.setCreateTime(PlatformDateUtils.getCurrentDate());
            entity.setCreateUser(PlatformSessionContext.getUserID());
            entity.setCreateUserName(dto.getUserName());
            if(dto.getRole() == 1){
                entity.setProvince(dto.getProvince());
                entity.setProvinceName(dto.getProvinceName());
                entity.setCity(dto.getCity());
                entity.setCityName(dto.getCityName());
                entity.setCounty(dto.getCounty());
                entity.setCountyName(dto.getCountyName());
            }
            entity.setId(UUIDGenerator.getUUID());
            entity.setRange(dto.getRole());
            entity.setDelFlag("0");
            entity.setSiteClassification(getKeyByValue("smxcdlb",objects.get(1).toString()));
            entity.setMaterial(getKeyByValue("smxdxgwgc",objects.get(2).toString()));
            entity.setJoint(getKeyByValue("smxdxgwjkxs",objects.get(3).toString()));
            entity.setDiameter(new BigDecimal(objects.get(4).toString()));
            entity.setYear(getKeyByValue("smxjsnd",objects.get(5).toString()));
            entity.setIntensity(getKeyByValue("smxsfqk",objects.get(6).toString()));
            entity.setDisasterRate6State(objects.get(7).toString());
            entity.setDisasterRate7State(objects.get(8).toString());
            entity.setDisasterRate8State(objects.get(9).toString());
            entity.setDisasterRate9State(objects.get(10).toString());
            entity.setDisasterRate10State(objects.get(11).toString());
            int count = lifelineFoundationLibraryRepository.getNetworkCountByCondition(entity);
            if (count != 0) {
                list.add(i);
            } else {
                lifelineFoundationLibraryRepository.saveUpNetwork(entity);
            }
            i += 1;
        }
        return RestResponse.succeed(list);
    }

    private RestResponse upNetworkVerification(int size, List<List<Object>> data) {
        for (int i = 0; i < size; i++) {
            List<Object> objects = data.get(i);
            if (StringUtils.isBlank(objects.get(1).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{场地类别}不能为空！");
            if (StringUtils.isBlank(objects.get(2).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{管材}不能为空！");
            if (StringUtils.isBlank(objects.get(3).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{接口形式}不能为空！");
            if (StringUtils.isBlank(objects.get(4).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{管径（mm）}不能为空！");
            if (StringUtils.isBlank(objects.get(5).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{建造年代}不能为空！");
            if (StringUtils.isBlank(objects.get(6).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{设防情况}不能为空！");
            if (StringUtils.isBlank(objects.get(7).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅵ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(8).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅶ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(9).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅷ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(10).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅸ度破坏状态}不能为空！");
            if (StringUtils.isBlank(objects.get(11).toString()))
                return RestResponse.fail("第" + (i + 5) + "行的{Ⅹ度破坏状态}不能为空！");
        }
        return RestResponse.succeed();
    }

    @Override
    public Integer getRole() {
        String role = roleService.getRoleByUserId(PlatformSessionContext.getUserID());
        List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
        ArrayList<String> roleList = new ArrayList<>();
        for (Map roleMap : roleMaps) {
            String roleCode = (String) roleMap.get("roleCode");
            roleList.add(roleCode);
        }
        if (roleList.contains(FxfzConstants.CZT_COUN_GL_ROLE)) {
            return 0;
        } else if (roleList.contains(FxfzConstants.CZT_PRI_GL_ROLE)) {
            return 1;
        }
        return 2;
    }

    @Override
    public HashMap<String, Object> page(LifelineFoundationLibraryParam param, int page, int size) {
        String user = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        Integer role = getRole();
        return lifelineFoundationLibraryRepository.page(param, page, size, sUser.getProvinceId(),role);
    }

    @Override
    public DestructiveStateVo getByTypeAndLifelineIdAndIntensity(String id, Integer type,Integer range) {
        Map<String, Object> map = lifelineEngineeringRepository.getBasicByIdAndType(type, id);
        if (PlatformObjectUtils.isNotEmpty(map)) {
            SmxVersionEntity entity = lifelineEngineeringRepository.getById(map.get("version_id").toString());
            if(type!=8 && type!=9){
                Integer year = Integer.parseInt(map.get("year").toString());
                map.put("year",getKeyByValue("smxjsnd",MainTableUtils.getYear(year)));
            }
            map.put("province",entity.getProvince());
            map.put("city",entity.getCity());
            map.put("county",entity.getCounty());
            return lifelineFoundationLibraryRepository.getByBasicInfo(map, type, range);
        }
        return new DestructiveStateVo();
    }

    @Override
    public void updateById(List<updateBasicLibraryParam> list) {
        list.forEach(it -> {
            lifelineFoundationLibraryRepository.updateById(it);
        });
    }

    @Override
    public void deleteByTypeAndIds(Integer type, String ids) {
        for (String id : ids.split(",")) {
            lifelineFoundationLibraryRepository.deleteByTypeAndIds(type, id);
        }
    }
}
